<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>字典类型</span>
        <el-button style="float: right;" type="primary" size="mini" @click="saveDictTypeVisible=true">新增</el-button>
      </div>
      <el-table highlight-current-row :data="data.records" @current-change="currentChange">
        <el-table-column prop="code" label="编码" width="150"> </el-table-column>
        <el-table-column prop="name" label="名称" width="150"> </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="100" width="auto"> </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showUpdate(scope.row.code)">编辑</el-button>
            <el-popconfirm title="确定要删除吗？" @onConfirm="remove(scope.row.code)">
              <el-button style="margin-left: 10px;" type="danger" size="mini" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="$config.defaultPageSizeOpts"
        :page-size="size"
        layout="total, sizes, prev, pager, next"
        :total="data.total">
      </el-pagination>
    </el-card>
    <SaveDictType :visible="saveDictTypeVisible"
              @on-close="saveDictTypeVisible=false"
              @on-success="saveSuccess">
    </SaveDictType>
    <UpdateDictType :visible="updateDictTypeVisible"
                :code="code"
                @on-close="updateDictTypeVisible=false"
                @on-success="updateSuccess">
    </UpdateDictType>
  </div>
</template>
<script>

import { mapActions } from 'vuex'
import SaveDictType from '@/views/system/dict/dict-type/SaveDictType'
import UpdateDictType from '@/views/system/dict/dict-type/UpdateDictType'

export default {
  name: 'DictType',
  components: { UpdateDictType, SaveDictType },
  data() {
    return {
      data: {},
      params: {},
      form: {},
      current: 1,
      size: 10,
      saveDictTypeVisible: false,
      updateDictTypeVisible: false,
      code: '',
      loading: false
    }
  },
  methods: {
    ...mapActions(['GetDictTypePage', 'RemoveDictTypeById']),
    search() {
      this.params.current = this.current
      this.params.size = this.size
      this.loading = true
      this.GetDictTypePage(this.params).then(res => {
        this.loading = false
        if (res) {
          this.data = res
        }
      }).catch(() => {
        this.loading = false
      })
    },
    reset() {
      this.params = {}
      this.current = 1
    },
    searchByCondition() {
      this.current = 1
      this.search()
    },
    handleSizeChange(val) {
      this.size = val
      this.search()
    },
    handleCurrentChange(val) {
      this.current = val
      this.search()
    },
    saveSuccess() {
      this.search()
      this.saveDictTypeVisible = false
    },
    showUpdate(code) {
      this.code = code
      this.updateDictTypeVisible = true
    },
    updateSuccess() {
      this.search()
      this.updateDictTypeVisible = false
    },
    remove(code) {
      this.RemoveDictTypeById(code).then(() => {
        this.search()
      })
    },
    currentChange(val) {
      this.$emit('on-select', val)
    }
  },
  mounted() {
    this.search()
  }
}
</script>
